package com.leo.foodmaster.admin.recipe.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.leo.foodmaster.commons.entity.Result;
import com.leo.foodmaster.commons.util.WebUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.recipe.dto.RecipeTypeDTO;
import com.leo.foodmaster.admin.recipe.entity.RecipeTypeEntity;
import com.leo.foodmaster.admin.recipe.mapper.RecipeTypeMapper;
import com.leo.foodmaster.admin.recipe.service.IRecipeTypeService;
import lombok.AllArgsConstructor;

import java.util.List;

/**
 * 菜品类型
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2021-10-29
 */
@Service
@AllArgsConstructor
public class IRecipeTypeServiceImpl extends ServiceImpl<RecipeTypeMapper, RecipeTypeEntity> implements IRecipeTypeService {

	private final RecipeTypeMapper recipeTypeMapper;
	@Override
	public IPage<RecipeTypeEntity> pageList(IPage<RecipeTypeDTO> page, String name) {
		Long organId = WebUtils.getUser().getOrganId();
		LambdaQueryWrapper<RecipeTypeEntity> eq = Wrappers.lambdaQuery(RecipeTypeEntity.class)
				.eq(RecipeTypeEntity::getOrganId, organId)
				.like(StringUtils.isNotBlank(name), RecipeTypeEntity::getName, name);
		return recipeTypeMapper.selectPage(new Page<>(page.getCurrent(),page.getSize()), eq);
	}

	/**
	 * 删除菜品分类
	 * @param ids 分类ids
	 * @return String
	 */
	@Override
	public Result<Object> deleteBatchIds(List<Long> ids) {
		// 正在使用的菜品分类不能删
		String typeNames = recipeTypeMapper.checkForUse(ids);
		if (StringUtils.isNotBlank(typeNames)) {
			return Result.fail("菜品分类【" + typeNames + "】正在被使用，不能删除!");
		}
		recipeTypeMapper.deleteBatchIds(ids);
		return Result.success();
	}

}
